package com.wangyadong.hobby.javanetwork.urlconnection;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class UrlConnectionTest {
    static URL url;
    static URLConnection urlConnection;

    static {
        try {
            url = new URL("https://blog.csdn.net/qq_21808961/article/details/81561464");
            urlConnection = url.openConnection();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws IOException {
        getAllHead(args);
    }

    public static void openConnection(String[] args) throws IOException {


        String contentType = urlConnection.getContentType();
        String contentEncoding = urlConnection.getContentEncoding();
        int contentLength = urlConnection.getContentLength();
        Long contentLengtLong = urlConnection.getContentLengthLong();
        long date = urlConnection.getDate();
        System.out.println("contentType is " + contentType);
        System.out.println("contentEncoding is " + contentEncoding);
        System.out.println("contentLength is " + contentLengtLong);
        System.out.println("date is " + date);
    }

    public static void getAllHead(String[] args) {
        for (int i = 0; ; i++) {
            String headerField = urlConnection.getHeaderField(i);
            if (headerField == null) break;
            System.out.println("第" + i + "个首部是 " + headerField);
        }
    }
}
